package com.rzjm.api.scheduler;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rzjm.api.entity.*;
import com.rzjm.api.service.*;
import com.rzjm.api.util.DateUtil;
import com.rzjm.api.util.WxMessage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Component
public class DataAnalysis {
    private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Resource
    private UserMpService userMpService;
    @Resource
    private ParameterService parameterService;
    @Resource
    private IDeviceWaterOrderService deviceWaterOrderService;
    @Resource
    private IDeviceWaterService deviceWaterService;
    @Resource
    private WxTemplateService wxTemplateService;

    @Value("${web.appidMp}")
    private String appidMp;//图片地址
    @Value("${web.secretMp}")
    private String secretMp;//图片地址

    @Value("${web.isLocal}")
    private String isLocal;//图片地址

    @Resource
    protected RedisTemplate<String, Object> redisTemplate;


    /**
     *  取消订单
     *  每2分钟执行一次
     *   15分钟之内未支付的订单
     */
    @Scheduled(cron = "0 0/2 * * * ?")
    public void checkWaterOrderStop() {
        if(isLocal.equals("0")){
            Integer[] status = { 0, 1 };
            QueryWrapper<DeviceWaterOrder> qw = new QueryWrapper<>();
            qw.in("status", Arrays.asList(status));
            List<DeviceWaterOrder> orderList = deviceWaterOrderService.list(qw);
            if(CollUtil.isNotEmpty(orderList)){
//                System.out.println("当前有" + orderList.size() + "个订单已支付和在充电中");
                for(DeviceWaterOrder waterOrder : orderList){
                    Date createTime = waterOrder.getCreateTime();
                    Date checkTime = DateUtil.getAfterSecondsByOld(createTime,2 * 60);
                    if(new Date().getTime() > checkTime.getTime()){
                        //订单 结算
                        waterOrder.setStatus(8);
                        deviceWaterOrderService.updateById(waterOrder);
                    }
                }
            }
        }
    }


    /**
     *  设备离线
     *  每10分钟执行一次
     *   15分钟之内未支付的订单
     */
    @Scheduled(cron = "0 0/2 * * * ?")
    public void checkChargerOnLineState() {
        if(isLocal.equals("0")) {
            QueryWrapper<DeviceWater> qw = new QueryWrapper<>();
            qw.eq("state",1);
            qw.lt("last_online_time", DateUtil.getAfterSecondsByOld(new Date(), -(5 * 60)));
            List<DeviceWater> chargers = deviceWaterService.list(qw);
            if (CollUtil.isNotEmpty(chargers)) {
                System.out.println("当前有" + chargers.size() + "台设备离线");
                for (DeviceWater water : chargers) {
                    water.setOnlineState(0);
                    water.setLastOfflineTime(new Date());
                    deviceWaterService.updateById(water);
                    //发送离线预警
                    wxTemplateService.sendWxMsgOfflineWater(water);
                }
            }
        }
    }
}
